home *** CD-ROM | disk | FTP | other *** search
-
- /* --------------------------------------------------------------
-
- FUNCTION ADD_NODE: The steps to add a new node to the list in
- ascending alphabetic order are:
-
- A. Get a string from the user.
-
- B. If this string already exists in the list increment its occurrence
- count and get out.
-
- C. Have a unique string so get a free node from free node list. If no
- more, complain and get out.
-
- D. Get space for new node's string. If no more, complain and get out.
-
- E. Initialize new node's string pointer and count members, and copy
- user string to allocated space. Also increment active node count.
-
- F. If this is the first node, make its fwd and bwd pointers go
- nowhere and make both root and tail pointers point to it.
-
- G. Else if its string is less than that of the first node, insert it
- at the beginning of the list by initializing the newly inserted node,
- the bwd pointer of the old first node, and adjusting the root pointer.
-
- H. Else the new node is to be inserted after an existing link.
-
- I. And if the new node is inserted after the last node in the list,
- adjust the tail pointer accordingly.
-
- -------------------------------------------------------------- */
-
- void add_node(void)
- {
- Node *pnew_node; /* ptr to new node */
- Node *ploc_node; /* ptr to located node */
- char *pstring; /* ptr to alloced string space */
- char string[21]; /* tmp holder for node's string */
-
- /*A*/ printf("\n Enter new string: ");
- scanf("%20s", string);
-
- /*B*/ ploc_node = locate_node(string, EXACT);
- if (ploc_node != NULL) {
- ++ploc_node->count;
- printf("\n Duplicate added");
- return;
- }
-
- /*C*/ pnew_node = get_free_node();
- if (pnew_node == NULL) {
- printf("\n No nodes available\n");
- return;
- }
-
- /*D*/ pstring = malloc(strlen(string) + 1);
- if (pstring == NULL) {
- printf("\n No string space available\n");
- return;
- }
-
- /*E*/ ++nodes_in_use;
- pnew_node->pstring = pstring;
- pnew_node->count = 1;
- strcpy(pnew_node->pstring, string);
-
- /*F*/ if (proot_node == NULL) {
- pnew_node->pfwd = NULL;
- pnew_node->pbwd = NULL;
- proot_node = pnew_node;
- ptail_node = pnew_node;
- }
- else {
- /*G*/ ploc_node = locate_node(string, INEXACT);
- if (ploc_node == NULL) {
- pnew_node->pfwd = proot_node;
- pnew_node->pbwd = NULL;
- proot_node->pbwd = pnew_node;
- proot_node = pnew_node;
- }
- /*H*/ else {
- pnew_node->pfwd = ploc_node->pfwd;
- pnew_node->pbwd = ploc_node;
- ploc_node->pfwd = pnew_node;
- if (pnew_node->pfwd != NULL) {
- pnew_node->pfwd->pbwd = pnew_node;
- }
- /*I*/ else {
- ptail_node = pnew_node;
- }
- }
- }
- printf("\n Node added");
- }
-
-